package kr.co.nowgugudan;

import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListDBbase extends Activity {
		ArrayList<MyItem>arItem;
		WordDBHelper mHelper;
		SQLiteDatabase db;
		ContentValues row;
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.listview);
			arItem = new ArrayList<MyItem>();
			
			mHelper = new WordDBHelper(this);
			
			db = mHelper.getReadableDatabase();	 
			Cursor cursor;
			// SQL 명령으로 읽기 
			//cursor = db.rawQuery("select * from dir where idx = ? order by desc limit 10;",null);
			cursor = db.rawQuery("SELECT a,b,c FROM dic ORDER BY b DESC limit 10", null);
			MyItem mi;
			while(cursor.moveToNext()) {
				String a = cursor.getString(0);
				String b = cursor.getString(1);
				String c = cursor.getString(2);
				mi=new MyItem(a,b,c);
				arItem.add(mi);
			}
		
			
			cursor.close();
			mHelper.close();
			
			MyListAdapter MyAdapter = new MyListAdapter(this, R.layout.recorddbsql, arItem);
			
			ListView MyList;
			MyList=(ListView) findViewById(R.id.list);
			MyList.setAdapter(MyAdapter);
	
			
			
		/*	db = mHelper.getWritableDatabase();	
			// SQL 명령으로 삭제
			db.execSQL("DELETE FROM dic;");
			mHelper.close();*/
			}
			
	}
		class MyItem {
			String name1;
			String name2;
			String name3;
			
			MyItem(String aname1, String aname2, String aname3) {
				super();
				
				name1 = aname1;
				name2 = aname2;
				name3 = aname3;
			}
			
		}
			class MyListAdapter extends BaseAdapter{
				Context maincon;
				LayoutInflater Inflater;
				ArrayList<MyItem>arSrc;
				int layout;
				
				public MyListAdapter(Context context,int alayout,ArrayList<MyItem>aarSrc){
					maincon=context;
					Inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					
					arSrc=aarSrc;
					layout=alayout;
				
				}
				
				public int getCount() {
					return arSrc.size();
				}

				public String getItem(int position) {
					return arSrc.get(position).name1+
						   arSrc.get(position).name2+
						   arSrc.get(position).name3;
				}

				public long getItemId(int position) {
					return position;
				}

				public View getView(int position, View converView, ViewGroup parent) {
					final int pos = position;
					if(converView == null){
						converView=Inflater.inflate(layout, parent,false);
					}
					TextView txt1=(TextView) converView.findViewById(R.id.text1001);
					txt1.setText(arSrc.get(position).name1);
					
					TextView txt2=(TextView) converView.findViewById(R.id.text1002);
					txt2.setText(arSrc.get(position).name2);
					
					TextView txt3=(TextView) converView.findViewById(R.id.text1003);
					txt3.setText(arSrc.get(position).name3);
					
					return converView;
				}
			
			
		}
			

		

		
		
